package com.gexiaobao.pigeon.ui.fragment.mine.organization.activity

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseActivity
import com.gexiaobao.pigeon.app.ext.Constant
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.showMessage
import com.gexiaobao.pigeon.app.util.KvUtils
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.databinding.ActivityPrintVisitBinding
import com.gexiaobao.pigeon.ui.adapter.AdapterPrintVisitPigeon
import com.gexiaobao.pigeon.ui.dialog.RxDialogDefault
import com.gexiaobao.pigeon.viewmodel.ShedPigeonViewModel
import com.google.gson.Gson
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody

/**
 * @Author      : hxw
 * @Date        : 2024/7/10 13:22
 * @Describe    : 打印探视数据
 */
class ActivityPrintVisit : BaseActivity<ShedPigeonViewModel, ActivityPrintVisitBinding>() {

    private var printCode = ""
    private var mMemberId = 0
    private var mRaceId = 0
    private var mOrgUserId = 0

    private val mAdapter: AdapterPrintVisitPigeon by lazy { AdapterPrintVisitPigeon(arrayListOf()) }

    override fun initView(savedInstanceState: Bundle?) {
        StatusBarUtil.setTranslucentForImageView(this, 0, null)
        mDatabind.includeBar.tvTitle.text = "打印探视数据"

        printCode = intent.getStringExtra("printCode").toString()
        mMemberId = intent.getIntExtra("memberId", 0)
        mRaceId = intent.getIntExtra("raceId", 0)
        mOrgUserId = KvUtils.decodeInt(Constant.ORG_USER_ID)!!

        onRefresh()

        mDatabind.swipeRefreshLayout.init {
            onRefresh()
        }
        mDatabind.recyclerView.init(LinearLayoutManager(this), mAdapter)
    }

    private fun onRefresh() {
        showLoading(getString(R.string.loading))
        mViewModel.getVisitPrint(mRaceId, mMemberId)
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun createObserver() {
        super.createObserver()
        /** 确认打印 */
        mViewModel.confirmPrintResult.observe(this) {
            if (it.isSuccess) {
                RxToast.showToast("发送成功")
                finish()
            } else {
                showMessage(it.errorMsg)
            }
        }
        /** 获取探视打印信息 */
        mViewModel.printVisitResult.observe(this) {
            if (mDatabind.swipeRefreshLayout.isRefreshing) {
                mDatabind.swipeRefreshLayout.isRefreshing = false
            }
            mDatabind.data = it
            if (it != null) {
                mAdapter.setList(it.list)
                mAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.includeBar.ivBack, mDatabind.llPrintVisitData) {
            when (it) {
                mDatabind.llPrintVisitData -> showConfirmDialog()
                mDatabind.includeBar.ivBack -> finish()
            }
        }
    }

    private fun showConfirmDialog() {
        val dialog = RxDialogDefault(this)
        dialog.setTitle("确认打印当前信息?")
        dialog.setSureListener {
            showLoading(getString(R.string.loading))
            val map = mapOf(
                "userId" to mOrgUserId,
                "raceId" to mRaceId,
                "memberId" to mMemberId,
                "printCode" to printCode
            )
            val body: RequestBody = Gson().toJson(map).toRequestBody("application/json".toMediaTypeOrNull())
            mViewModel.confirmPrint(body)
            dialog.dismiss()
        }
        dialog.setFullScreenWidth()
        dialog.show()
    }
}